using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

namespace Moussavi
{
    class ImageConverter
    {
        private Bitmap m_ImageToConvert;
        private Bitmap m_ConvertedBitmap;
        private char[][][] m_ConvertedBuffer;

        public Bitmap ConvertedImage
        {
            get
            {
                return m_ConvertedBitmap;
            }
        }

        public char[][][] ConvertedBuffer
        {
            get
            {
                return m_ConvertedBuffer;
            }
        }

        public ImageConverter(Bitmap ImageToConvert, int VerticalResolution, int bNotRGB, int iColorDepth)
        {
            if (ImageToConvert == null) return;

            Color c, c1;

            int newR;
            int newG;
            int newB;
            int IBW;
            Color m_ColorOfLED = new Color();
            m_ColorOfLED = Color.FromArgb(255, 255, 255, 255);

            m_ImageToConvert = ImageToConvert;


            // Calculate parameters
            double dResizeRatio = (double)(VerticalResolution)/(double)(m_ImageToConvert.Height);
            int newHeight = VerticalResolution;
            int newWidth = (int)(dResizeRatio*(double)(m_ImageToConvert.Width));

            // Create converted buffer
            m_ConvertedBuffer = new char[newWidth][][];
            for (int i = 0; i < newWidth; i++)
            {
                m_ConvertedBuffer[i] = new char[newHeight][];
                for (int j = 0; j < newHeight; j++)
                {
                    m_ConvertedBuffer[i][j] = new char[3];
                }
            }
            
            // Convert
            m_ConvertedBitmap = new Bitmap(newWidth, newHeight);
            Graphics g = Graphics.FromImage((Image)(m_ConvertedBitmap));
            g.DrawImage(m_ImageToConvert, 0, 0, newWidth, newHeight);

            // Convert Further?
            for (int x = 0; x < newWidth; x++)
            {
                //ftheta = 0;
                for (int y = 0; y < newHeight; y++)
                {
                    Color c_inter = m_ConvertedBitmap.GetPixel(x, y);

                    if (bNotRGB==0) // Its an RGB system
                    {
                        newR = c_inter.R / (int)(256.0 / (double)(iColorDepth)) * (int)(256.0 / (double)(iColorDepth));
                        newG = c_inter.G / (int)(256.0 / (double)(iColorDepth)) * (int)(256.0 / (double)(iColorDepth));
                        newB = c_inter.B / (int)(256.0 / (double)(iColorDepth)) * (int)(256.0 / (double)(iColorDepth));
                        m_ConvertedBuffer[x][y][0] = (char)(c_inter.R / (int)(256.0 / (double)(iColorDepth)));
                        m_ConvertedBuffer[x][y][1] = (char)(c_inter.G / (int)(256.0 / (double)(iColorDepth)));
                        m_ConvertedBuffer[x][y][2] = (char)(c_inter.B / (int)(256.0 / (double)(iColorDepth)));
                        m_ConvertedBitmap.SetPixel(x, y, Color.FromArgb(150, newR, newG, newB));
                    }
                    else // BW image
                    {
                        int DividerR = (int)((double)(m_ColorOfLED.R + 1) / (double)(iColorDepth));
                        int DividerG = (int)((double)(m_ColorOfLED.G + 1) / (double)(iColorDepth));
                        int DividerB = (int)((double)(m_ColorOfLED.B + 1) / (double)(iColorDepth));

                        IBW = (int)(c_inter.R * 0.3 + c_inter.B * 0.4 + c_inter.G * 0.4);
                        if ((int)((double)(m_ColorOfLED.R) / (double)(iColorDepth)) != 0)
                        {
                            newR = IBW / (int)((double)(m_ColorOfLED.R) / (double)(iColorDepth)) * (int)((double)(m_ColorOfLED.R) / (double)(iColorDepth));
                        }
                        else newR = 0;

                        if ((int)((double)(m_ColorOfLED.G) / (double)(iColorDepth)) != 0)
                        {
                            newG = IBW / (int)((double)(m_ColorOfLED.G) / (double)(iColorDepth)) * (int)((double)(m_ColorOfLED.G) / (double)(iColorDepth));
                        }
                        else newG = 0;

                        if ((int)((double)(m_ColorOfLED.B) / (double)(iColorDepth)) != 0)
                        {
                            newB = IBW / (int)((double)(m_ColorOfLED.B) / (double)(iColorDepth)) * (int)((double)(m_ColorOfLED.B) / (double)(iColorDepth));
                        }
                        else newB = 0;

                        c = Color.FromArgb(150, (newR > 255) ? 255 : newR, (newG > 255) ? 255 : newG, (newB > 255) ? 255 : newB);
                        c1 = Color.FromArgb(255, ((newR > 255) ? 255 : newR) / DividerR, ((newG > 255) ? 255 : newG) / DividerG, ((newB > 255) ? 255 : newB) / DividerB);

                        m_ConvertedBitmap.SetPixel(x,y, c); 
                        m_ConvertedBuffer[x][y][0] = (char)(c1.R);
                        m_ConvertedBuffer[x][y][1] = (char)(c1.G);
                        m_ConvertedBuffer[x][y][2] = (char)(c1.B);
                    }
                    
                }
            }
        }
    }
}
